using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SolarWinds.Helpers;
using SolarWinds.Input;

namespace SolarWinds.Widgets
{
    public class LabelSpinBox : TiledWidget
    {
        public event EventHandler ValueChanged;

        private enum CursorArea 
        {
            None,
            Left,
            Right,
            Middle
        }

        private CursorArea mCursorArea = CursorArea.None;
        public event EventHandler Clicked;

        private string[] mPossibleValues = new[] { "" };

        public string[] PossibleValues
        {
            get { return mPossibleValues; }
            set { mPossibleValues = value; }
        }

        private int mCurrentIndex;

        public int CurrentIndex
        {
            get { return mCurrentIndex; }
            set 
            {
                if (CurrentIndex != value)
                {
                    // torus 
                    if (value < 0)
                    {
                        mCurrentIndex = mPossibleValues.Length - 1;
                    }
                    else if (value >= mPossibleValues.Length)
                    {
                        mCurrentIndex = 0;
                    }
                    else
                    {
                        mCurrentIndex = value;
                    }

                    if (ValueChanged != null)
                    {
                        ValueChanged(this, null);
                    }
                }
            }
        }

        public string CurrentValue
        {
            get { return mPossibleValues[mCurrentIndex]; }
            set
            {
                for (int i = 0; i < mPossibleValues.Length; ++i)
                {
                    if (mPossibleValues[i] == value)
                    {
                        CurrentIndex = i;
                        break;
                    }
                }
            }
        }

        public LabelSpinBox(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
        }

        protected internal override void PaintEvent()
        {
            base.PaintEvent();

            // strip string
            String stripCaption = SpriteFontHelper.StripString(Font, mPossibleValues[mCurrentIndex], Width - 2 * mTileSize.X);

            Vector2 fontSize = Font.MeasureString(stripCaption);

            var pos = new Vector2(ScreenPosition.X + (Width - fontSize.X) / 2,
                                  ScreenPosition.Y + (Height - fontSize.Y) / 2);

            SpriteBatch.DrawString(Font, stripCaption, pos, TextColor);
        }

        protected override void InitializeRects()
        {
            mSrcRects = new Rectangle[2];
            mDestRects = new Rectangle[2];
            mTileColors = new Color[2] { Color.White, Color.White };

            MinWidth = 2 * mTileSize.X;
            MinHeight = mTileSize.Y;
        }

        protected override void UpdateSrcDestRects()
        {
            // left button
            mSrcRects[0] = TileIndex2Rectangle(new Point(2, 5));
            mDestRects[0] = new Rectangle(ScreenPosition.X, ScreenPosition.Y, mTileSize.X, Height);
            // right button
            mSrcRects[1] = TileIndex2Rectangle(new Point(3, 5));
            mDestRects[1] = new Rectangle(ScreenPosition.X + Width - mTileSize.X, ScreenPosition.Y, mTileSize.X, Height);
        }

        protected internal override void MouseMoveEvent(MouseEvent evt)
        {
            Point absPos = evt.AbsolutePosition;

            // let's find out where our cursor is ...
            if (CheckTile(0, absPos)) mCursorArea = CursorArea.Left;
            else if (CheckTile(1, absPos)) mCursorArea = CursorArea.Right;
            else mCursorArea = CursorArea.None;

            if (evt.IsConsumed)
            {
                return;
            }
        }

        private bool CheckTile(int i, Point absPos)
        {
            if (GeometryHelper.Contains(mDestRects[i], absPos))
            {
                mTileColors[i] = Color.Gray;

                return true;
            }
            
            mTileColors[i] = Color.White;

            return false;
        }

        protected internal override void MouseClickEvent(MouseEvent evt)
        {
            if (mCursorArea == CursorArea.Left)
            {
                CurrentIndex--;
            }
            else if (mCursorArea == CursorArea.Right)
            {
                CurrentIndex++;
            }
        }

        protected internal override void KeyClickedEvent(KeyEvent evt)
        {
            if (evt.Key == Microsoft.Xna.Framework.Input.Keys.Left)
            {
                CurrentIndex--;
            }
            else if (evt.Key == Microsoft.Xna.Framework.Input.Keys.Right)
            {
                CurrentIndex++;
            }
        }

        protected internal override void KeyFocusInEvent(KeyEvent evt)
        {
            mTileColors[0] = Color.CornflowerBlue;
            mTileColors[1] = Color.CornflowerBlue;
        }

        protected internal override void KeyFocusOutEvent(KeyEvent evt)
        {
            mTileColors[0] = Color.White;
            mTileColors[1] = Color.White;
        }
    }
}